package cn.edu.lnsf.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

import cn.edu.lnsf.model.Orders;
import cn.edu.lnsf.model.Room;
import cn.edu.lnsf.service.IbookroomService;
import cn.edu.lnsf.service.IordersService;
import cn.edu.lnsf.service.IroomService;
import cn.edu.lnsf.service.impl.BookroomServiceImpl;
import cn.edu.lnsf.service.impl.OrdersServiceImpl;
import cn.edu.lnsf.service.impl.RoomServiceImpl;

public class BookRoomController {	
	public IbookroomService service= new BookroomServiceImpl();
	public IroomService rservice = new RoomServiceImpl();
	private IordersService oservice = new OrdersServiceImpl();
	public Scanner scanner = new Scanner(System.in);
	public OnlineCsController oc = new OnlineCsController();
	public OnlineOrdersController onlineod = new OnlineOrdersController();
	public RoomController roomcon = new RoomController();	
	
	Orders ot = new Orders();
	public void bookroom(Orders ot) {		
		List<Orders> list2 = new ArrayList<Orders>();
		@SuppressWarnings("resource")
		Scanner scanner = new Scanner(System.in);
		int choose = -1;
		while(choose!=0){
			System.out.println();
			System.out.println("\t\t\t\t\t  ❤☆☆☆☆☆☆☆☆☆☆☆☆☆☆请选择您需要的服务☆☆☆☆☆☆☆☆☆☆☆☆☆☆❤");	
			System.out.println();
			System.out.println("\t\t\t\t\t                       1、查看房间信息 				              ");
			System.out.println("\t\t\t\t\t                       2、浏览购物车                                       ");
			System.out.println("\t\t\t\t\t                       3、预订房间                                            ");
			System.out.println("\t\t\t\t\t                       4、删除单个预订房                                  ");
			System.out.println("\t\t\t\t\t                       5、清空购物车 				          ");
			System.out.println("\t\t\t\t\t                       6、提交 				              ");			
			System.out.println("\t\t\t\t\t                       0、退出 				           	");
			System.out.println();
			System.out.println("\t\t\t\t\t  ❤☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆❤");
			System.out.println();
			System.out.print("\t\t\t\t❤请输入要操作的数字：");	
			choose = scanner.nextInt();
			System.out.println();
			switch(choose){
				case 1:
					roomcon.findAllRoom();
					break;
				case 2:
					service.getList();
					break;
				case 3:		
				{				
					Room r = new Room();
					String roomType = null;
					String roomNumber = null;
					String checkOutTime = null, checkInTime = null;
					Orders o1 = new Orders();
					System.out.println();
					System.out.print("\t\t\t\t❤请输入入住时间：");
					checkInTime = scanner.next();
					System.out.print("\t\t\t\t❤请输入退房时间：");
					checkOutTime = scanner.next();
					SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
					try {
						Date Indate = format.parse(checkInTime);
						o1.setCheckInTime(Indate);
						Date Outdate = format.parse(checkOutTime);
						o1.setCheckOutTime(Outdate);
					} catch (ParseException e) {
						System.out.println("\t\t\t\t\t\t\t✪       不合法的输入     ✪");
						e.printStackTrace();
					}
					
					System.out.println();
					System.out.println("\t\t\t\t\t  ❤☆☆☆☆☆☆☆☆☆☆☆☆☆☆我们提供的房间有☆☆☆☆☆☆☆☆☆☆☆☆☆☆❤");
					rservice.CountRoomType();
					System.out.println("\t\t\t\t\t  ❤☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆❤");
					System.out.println();
					System.out.println("\t\t\t\t\t      ❤☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆房间类型☆☆☆☆☆☆☆☆☆☆☆☆☆☆❤");
					System.out.println("\t\t\t\t\t      ❤                 优品单人房、优品双人房、豪华套房、总统套房       ❤          ");
					System.out.println("\t\t\t\t\t      ❤☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆❤");
					System.out.println();
					System.out.print("\t\t\t\t❤请输入房间类型：");
					roomType = scanner.next();
					System.out.print("\t\t\t\t❤分配的房间号：");					
					roomNumber = roomcon.findRoomNumberByroomType(roomType);
					System.out.println("\t"+roomNumber);
					r.setRoomNumber(roomNumber);
					o1.setRoomNumber(roomNumber);
					System.out.print("\t\t\t\t❤总住宿天数为：  ");
					int day = (int) oservice.totalAllMoney(o1);
					System.out.print("\t\t\t\t❤房间价格：");
					System.out.println(roomcon.findPriceByroomNumber(roomNumber)* day);
					r.setRoomPrice(roomcon.findPriceByroomNumber(roomNumber)* day);
					o1.setTotalMoney(roomcon.findPriceByroomNumber(roomNumber)* day);
					
					
					service.add(r);	
					list2.add(o1);				
				}		
				
					break;
				case 4:
					System.out.println();
					System.out.print("\t\t\t\t❤请选择要删除的房间号：");
					String roomNumber1 = scanner.next();
					service.delete(roomNumber1);
					for (Iterator<Orders> iter =  list2.iterator(); iter.hasNext();) {
						Orders o1 = iter.next();
						if (o1.getRoomNumber().equals(roomNumber1)) {
							iter.remove();
						}
					}
					break;
				case 5:
					service.clear();
					list2.clear();
					break;
				case 6:					
					System.out.println();
					for(Orders orders : list2){
						orders.setCustomerIDCard(ot.getCustomerIDCard());
						onlineod.insertOrders(orders);
					}	
					System.out.println("\t\t\t\t\t\t\t✪       您已成功预订酒店！    ✪");
					System.out.println("\t\t\t\t\t\t\t❤            共需支付"+service.getTotalPrice()+"元      ❤ ");
					service.clear();
					list2.clear();
					break;
				case 0:break;					
				default:
					System.out.println("\t\t\t\t\t\t\t✪       没有此项选择！    ✪");
					break;
			}
		}
		
	}	
	
} 
